package zhongbo.java.air.monitor.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.monitor.mapper.AirAlertLogMapper;
import zhongbo.java.air.monitor.model.AirAlertLog;
import zhongbo.java.air.monitor.service.IAirAlertLogService;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class AirAlertLogServiceImpl implements IAirAlertLogService {
    @Autowired
    AirAlertLogMapper airAlertLogMapper;
    @Override
    public int addAirAlertLog(AirAlertLog airAlertLog) {
        if (airAlertLog == null || airAlertLog.equals("")){
            throw new RuntimeException("NULL");
        }
        int insert = airAlertLogMapper.insert(airAlertLog);
        return insert;
    }

    @Override
    public void deleteAirAlertLog(String id) {
        if (id==null){
            throw new RuntimeException("NULL");
        }
        int i = airAlertLogMapper.deleteById(id);
    }

    @Override
    public int updateAirAlertLog(AirAlertLog airAlertLog) {
        if (airAlertLog == null || airAlertLog.equals("")){
            throw new RuntimeException("NULL");
        }
        AirAlertLog alertLog = airAlertLogMapper.selectById(airAlertLog.getId());
        if (alertLog== null || airAlertLog.equals("")){
            throw new RuntimeException("无此数据");
        }
        int update = airAlertLogMapper.updateById(airAlertLog);
        return update;
    }

    @Override
    public AirAlertLog selectById(String id) {
        if (id == null){
            throw new RuntimeException("NULL");
        }
        AirAlertLog airAlertLog = airAlertLogMapper.selectById(id);
        return airAlertLog;
    }

    @Override
    public PageVo page(String status, Integer pageNumber, Integer pageSize) {
        QueryWrapper<AirAlertLog> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(status)){
            queryWrapper.like("status",status);
        }
        IPage<AirAlertLog> pageParam = new Page<>(pageNumber,pageSize);
        IPage<AirAlertLog> pathPage =airAlertLogMapper.selectPage(pageParam,queryWrapper);
        return new PageVo<>(pathPage);
    }

    @Override
    public boolean hasRecentAlert(String icaoCode, Date triggerTime) {
        Date fiveMinutesAgo = new Date(triggerTime.getTime() - TimeUnit.MINUTES.toMillis(5));
        QueryWrapper<AirAlertLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("icao_code",icaoCode)
        .ge("trigger_time",fiveMinutesAgo).le("trigger_time",triggerTime);
        boolean b = airAlertLogMapper.selectCount(queryWrapper) > 0;
        return b;
    }

    @Override
    public List<AirAlertLog> findRecentAlerts(String icaoCode, Date startTime, Date endTime) {
        QueryWrapper<AirAlertLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("icao_code",icaoCode).between("trigger_time",startTime,endTime);
        List<AirAlertLog> airAlertLogs = airAlertLogMapper.selectList(queryWrapper);
        return airAlertLogs;
    }
}
